package com.glory.hzlionpda.dialog;


import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.adapter.MessageListAdapter;
import com.glory.hzlionpda.bean.MessageBean;
import com.glory.hzlionpda.bean.MessageObject;
import com.glory.hzlionpda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author hx
 * @date 2019/10/21
 * @desc
 */
public class MessageDialog extends AppCompatDialogFragment {
    RecyclerView rvMessageList;
    private List<MessageObject> messageEntityList;
    private MessageListAdapter messageListAdapter;
    private Timer timer;
    private FreeUI_CommonCallback callBack;
    private String eqpId;

    @Override
    public void show(FragmentManager manager, String tag) {
        if (!isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout((int) (FreeApi_StaticMembers.SCREEN_WIDTH * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        startTimer();
    }

    private void startTimer() {
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    requestMessageData();
                }
            }, 0, 2000);
        }
    }

    private void requestMessageData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EQPID", eqpId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestGetNet("LOGMESSAGERQUEST", this, jsonObject, new FreeUI_EntityCallBack<MessageBean>(
                new TypeToken<MessageBean>() {
                }.getType()) {
            @Override
            public void onSuccess(Response<MessageBean> response) {
                if (response.body().isSuccess(getActivity()) && !isHidden()) {
                    messageEntityList.clear();
                    messageEntityList.addAll(response.body().getLOGMESSAGETEXTLIST());
                    messageListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<MessageBean> response) {
                super.onError(response);
            }
        });
    }

    public void setCallBack(FreeUI_CommonCallback callBack, String eqpID) {
        this.eqpId = eqpID;
        this.callBack = callBack;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_message, null);
        rvMessageList = view.findViewById(R.id.rvMessageList);
        rvMessageList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvMessageList.setItemAnimator(new DefaultItemAnimator());
        rvMessageList.addItemDecoration(new DividerItemDecoration(getActivity(), 1));
        messageEntityList = new ArrayList<>();
        messageListAdapter = new MessageListAdapter(messageEntityList);
        messageListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        messageListAdapter.isFirstOnly(true);
        rvMessageList.setAdapter(messageListAdapter);
        TextView tvClose = view.findViewById(R.id.tvClose);
        tvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        getDialog().getWindow().setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.BottomDialog); // 添加动画
        getDialog().setCancelable(false);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        callBack.onSuccess();
        if (timer != null) {
            timer.cancel();
            OkGo.getInstance().cancelTag(this);
            timer = null;
        }
    }
}
